<template>
  <BasicModal
    v-bind="$attrs"
    :width="modalWidth"
    @register="registerModal"
    :title="getTitle"
    @ok="handleOK"
  >
    <!-- :labelWidth="100"
      :schemas="schemas"
      :actionColOptions="{ span: 24 }"
      @submit="handleSubmit"
      :showActionButtonGroup="showActionButtonGroup"
      v-if="current === 0" -->
    <BasicForm @register="registerForm">
    </BasicForm>
    <!-- :labelWidth="100"
      :schemas="schemasData"
      :actionColOptions="{ span: 24 }"
      :baseColProps="{ span: 24 }"
      @submit="handleSubmit"
      :showActionButtonGroup="showActionButtonGroup"
      v-if="current === 1" -->
    <!-- 
    <Form ref="formRef" :label-width="160" :model="formValue">
      <FormItem label="关键字" path="user.keyWord">
        <Input v-model:value="formValue.keyWord" placeholder="关键字" />
      </FormItem>
      <FormItem label="所需数量" path="user.maxNum">
        <Input v-model:value="formValue.maxNum" placeholder="所需数量" />
      </FormItem>
      <FormItem label="开始时间" path="DatePicker">
        <DatePicker
          v-model:formatted-value="formValue.timestamp"
          value-format="yyyy.MM.dd HH:mm:ss"
          type="datetime"
          clearable
        />
      </FormItem>
    </Form> -->
  </BasicModal>
</template>
<script lang="ts" setup>
  import { Steps, Form, Input, FormItem, Button, DatePicker, InputSearch } from 'ant-design-vue';
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { getDeptList } from '@/api/demo/system';
  import { accountFormSchema } from './account.data';
  import { createTaskListApi, listPageApi } from '@/api/demo/Task';
  import axios from 'axios';
  import { useI18n } from "@/hooks/web/useI18n";

  const { t } = useI18n();
  defineOptions({ name: 'AccountModal' });
  // const emit = defineEmits(['success', 'register']);
  const formValue = ref({
    maxNum: '',
    keyWord: '',
    timestamp: '',
  });
  const isUpdate = ref(true);
  const rowId = ref('');
  const modalWidth = ref('25%'); // 可以是像 600 这样的像素值，或者百分比 '50%'
  const emit = defineEmits(['success']);
  //注册表格数据
  const [registerForm, { resetFields, setFieldsValue, updateSchema, getFieldsValue }] = useForm({
    labelWidth: 90,
    baseColProps: { span: 24 },
    schemas: accountFormSchema,
    showActionButtonGroup: false,
  });
  //注册弹窗数据
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;

    if (unref(isUpdate)) {
      rowId.value = data.record.id;
      setFieldsValue({
        ...data.record,
      });
    }
    // const treeData = await getDeptList();
    // //更新表单中的schema，只更新函数所传的参数
    // updateSchema([
    //   {
    //     field: 'pwd',
    //     show: !unref(isUpdate),
    //   },
    //   {
    //     field: 'dept',
    //     componentProps: { treeData },
    //   },
    // ]);
  });
  const getTitle = computed(() => (!unref(isUpdate) ? t('sys.configuration.add') : t('sys.configuration.edit')));

  async function handleOK() {
    try {
      let data = await createTaskListApi({ ...getFieldsValue() });
      emit('success', data); // 触发 success 事件
      closeModal();
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }

  function handleValidateClick() {}
  async function onSearch(value: string) {
    try {
      // 发送请求到后端接口
      const response = await axios.get('http://192.168.0.128:5005/getWords', {
        params: { params: value }, // 将 value 包装在一个对象中
      });

      // 假设接口返回的数据中包含生成的关键字
      const keyword = response.data.keyword;

      // 更新表单字段
      setFieldsValue({
        field5: keyword, // 假设 field5 是关键字字段的名称
      });
    } catch (error) {
      console.error('生成关键字失败', error);
    }
  }
</script>
